//
//  KKRelateCell.m
//  DoMi
//
//  Created by 康凯 on 2016/11/10.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKRelateCell.h"
#import "KKStudyItem.h"
#import "KKItemListController.h"

@interface KKRelateCell()
@property(strong,nonatomic)NSArray *datas;
@property(assign,nonatomic)float scrollWidth;
@end

@implementation KKRelateCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.scrollView.scrollEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator=NO;
    self.scrollView.showsHorizontalScrollIndicator=NO;
}

-(void)initData:(NSArray *)datas relateTitle:(NSString *)title{
    self.relateTitle.text=title;
    if([title isEqualToString:@"相关文章"]){
        return;
    }
    if(datas!=nil&&datas.count>0){
        self.datas=datas;
        self.scrollWidth=0;
        for(int i=0;i<datas.count;i++){
            KKStudyItem *item=datas[i];
            CGSize textSize=[KKAppUtils sizeWithText:item.title font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(1000, 30)];
            UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(self.scrollWidth,0, textSize.width+20, 30)];
            self.scrollWidth+=textSize.width+20;
            btn.tag=i;
            //            btn.titleLabel.font=[UIFont boldSystemFontOfSize:14];
            btn.titleLabel.font=[UIFont systemFontOfSize:14];
            [btn setTitle:item.title forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithHexString:@"#787879"] forState:UIControlStateNormal];
            [self.scrollView addSubview:btn];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        self.scrollView.contentSize = CGSizeMake(self.scrollWidth, 30);
    }
    
}

-(void)btnClick:(UIButton *)btn{
    KKStudyItem *item= self.datas[btn.tag];
    KKItemListController *itemView=[[KKItemListController alloc] init];
    itemView.codeId=item.code;
    itemView.barTitle=item.title;
    UIViewController *currentVC=[KKAppUtils getCurrentVisiableVC:self];
    [currentVC.navigationController pushViewController:itemView animated:YES];
}

@end
